package no.progark.views;

import no.progark.helikopter.Direction;
import no.progark.helikopter.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.text.TextPaint;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class HelicopterView extends View {
	MediaPlayer sound;
	
	int HEIGHT;
	int WIDTH;
	
	//Gray helicopter
	int x;
	int y;
	Direction dir = Direction.DOWN;
	Paint paint = new Paint();
	Bitmap helicopter = BitmapFactory.decodeResource(getResources(), initImage(dir));
	boolean gcollision = false;
	boolean init = true;
	int imageId = initImage(dir);
	Paint textPaint = new Paint();
	
	//Blue helicopter
	
	int blueX;
	int blueY;
	Direction blueDir = Direction.LEFT;
	Paint blueTextPaint = new Paint();
	int blueImageId = R.drawable.bluecopterleft;
	Bitmap blueHelicopter = BitmapFactory.decodeResource(getResources(), R.drawable.bluecopterleft);
	boolean bcollision = false;
	
	//Green helicopter
	int greenX;
	int greenY;
	Direction greenDir = Direction.UP;
	Paint greenTextPaint = new Paint();
	int greenImageId = R.drawable.greencopterup;
	Bitmap greenHelicopter = BitmapFactory.decodeResource(getResources(), R.drawable.greencopterup);
	boolean greenCollision = false;
	
 
	public HelicopterView(Context context) {
		super(context); 
	
		playSound();
		
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		
		//Set background
		Rect bcg = new Rect();
		bcg.set(0, 0, canvas.getWidth(), canvas.getHeight());
		
		//Make paint
		paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setStyle(Paint.Style.FILL);
		
		//Render background with paint
		canvas.drawRect(bcg, paint);
		
		//Draw!
		drawGreenHelicopter(greenX, greenY, canvas);
		drawBlueHelicopter(blueX, blueY, canvas);
		drawGrayHelicopter(x, y, canvas);
		drawPositionCoordinates(x, y, canvas);
		
		bounceCollision();
		if(init){
			init(this);
			init = false;
		}
		else{updateCounter(canvas);}
		
		updateBlueCounter(canvas);
		updateGreenCounter(canvas);
		
		
		//Restart method
		invalidate();
	}
	
	public void init(View v){
		HEIGHT = v.getMeasuredHeight();
		WIDTH = v.getMeasuredWidth();
		x = (int) ( (Math.random() * v.getMeasuredWidth()));
		y  = (int) ((Math.random() * v.getMeasuredHeight()));
		blueX = (int) ((Math.random() * v.getMeasuredWidth()));
		blueY = (int) ((Math.random() * v.getMeasuredHeight()));
		greenX = (int) ( (Math.random() * v.getMeasuredWidth()));
		greenY = (int) ((Math.random() * v.getMeasuredHeight()));
	}
	
	
	private void drawGrayHelicopter(int x, int y, Canvas c){
		c.drawBitmap(helicopter, x,y, paint);	
	}
	private void drawBlueHelicopter(int x, int y, Canvas c){
		c.drawBitmap(blueHelicopter, x,y, paint);	
	}
	
	private void drawGreenHelicopter(int x, int y, Canvas c){
		c.drawBitmap(greenHelicopter, x,y, paint);	
	}
	public void drawPositionCoordinates(int x, int y, Canvas c){
		textPaint.setColor(Color.GRAY);
		textPaint.setStyle(Paint.Style.FILL);
		textPaint.setTextSize(16);
		c.drawText(""+x+","+y+"", 1*(WIDTH/10), 20, textPaint);
		
		blueTextPaint.setColor(Color.BLUE);
		blueTextPaint.setStyle(Paint.Style.FILL);
		blueTextPaint.setTextSize(16);
		c.drawText(""+blueX+","+blueY+"",4*(WIDTH/10),20,blueTextPaint);
		
		greenTextPaint.setColor(Color.GREEN);
		greenTextPaint.setStyle(Paint.Style.FILL);
		greenTextPaint.setTextSize(16);
		c.drawText(""+greenX+","+greenY+"",7 *(WIDTH/10),20,greenTextPaint);
	}
	
	public void bounceCollision(){
		if(Math.abs(blueX-x) < 50 && Math.abs(blueY-y) < 50){
			gcollision = true;
			bcollision = true;
		}
		if(Math.abs(blueX-greenX) < 50 && Math.abs(blueY-greenY) < 50){
			bcollision = true;
			greenCollision = true;
		}
		if(Math.abs(x-greenX) < 50 && Math.abs(y-greenY) < 50){
			gcollision = true;
			greenCollision = true;
		}
	}

	
	public void updateCounter(Canvas canvas){ 
		if(!sound.isPlaying())sound.start();
		if(dir==Direction.RIGHT){
			if(x+35 >= WIDTH || gcollision){
				dir = dir.getOppositeDirection();
				imageId = R.drawable.copterleft;
				helicopter = BitmapFactory.decodeResource(getResources(), R.drawable.copterleft); 
				x-=20;
				gcollision = false;
			}
			else{
				helicopter = BitmapFactory.decodeResource(getResources(), getNextImage());
				x+=4;
			}
		}
		if(dir==Direction.LEFT){
			if(x-5 < 0 || gcollision){
				dir=dir.getOppositeDirection();
				imageId = R.drawable.copterright;
				helicopter = BitmapFactory.decodeResource(getResources(), R.drawable.copterright); 
				x+=20;
				gcollision = false;
			}
			else{
				helicopter = BitmapFactory.decodeResource(getResources(), getNextImage());
				x-=4;
			}
		}
		if(dir==Direction.DOWN){
			if(y+40 > HEIGHT || gcollision){
				System.out.println("DOWN:");
				System.out.println("current dir: " + dir);
				dir=dir.getOppositeDirection();
				System.out.println("oppositeDir: " + dir );
				imageId = R.drawable.copterup;
				helicopter = BitmapFactory.decodeResource(getResources(), R.drawable.copterup); 
				y-=20;
				System.out.println(dir);
				gcollision = false;
			}
			else{
				helicopter = BitmapFactory.decodeResource(getResources(), getNextImage());
				y+=4;
			}
		}
		if(dir==Direction.UP){
			if(y-5 < 0|| gcollision){
				System.out.println("UP:");
				System.out.println("collision?? " + gcollision);
				System.out.println("current direction: " + dir);
				dir=dir.getOppositeDirection();
				System.out.println("new direction: " + dir);
				imageId = R.drawable.copterdown;
				helicopter = BitmapFactory.decodeResource(getResources(), R.drawable.copterdown); 
				y+=20;
				gcollision = false;
			}
			else{
				helicopter = BitmapFactory.decodeResource(getResources(), getNextImage());
				y-=4;
			}
		}
		
		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		onTouchChangeDirection(event.getX(), event.getY());
		return super.onTouchEvent(event);
	}
	
	public void playSound(){
		sound = MediaPlayer.create(getContext(), R.raw.heli_sound);
	}
	
	public int initImage(Direction d){
		int res = 0;
		switch (d.getDirectionCharacter()) {
		case 'u':
			res = R.drawable.copterup;
			break;
		case 'd':
			res = R.drawable.copterdown;
			break;
		case 'l':
			res = R.drawable.copterleft;
			break;
		case 'r':
			res = R.drawable.copterright;
			break;
		}
		return res;
	}
	
	public void onTouchChangeDirection(float xcoord, float ycoord){
		float dx = xcoord-x;
		float dy = ycoord-y;
		
		if(Math.abs(dx)>Math.abs(dy)){
			dir = (dx>0)?Direction.RIGHT:Direction.LEFT;
		}
		else{
			dir = (dy>0)?Direction.DOWN:Direction.UP;
		}
		init = false;
		imageId = initImage(dir);
		helicopter = BitmapFactory.decodeResource(getResources(), initImage(dir));
	}
	
	public int getNextImage(){
		int result = 0;
		switch(imageId){
		case R.drawable.copterdown:
			result = R.drawable.copterdown2;
			break;
		case R.drawable.copterdown2:
			result = R.drawable.copterdown;
			break;
		case R.drawable.copterup:
			result = R.drawable.copterup2;
			break;
		case R.drawable.copterup2:
			result = R.drawable.copterup;
			break;
		case R.drawable.copterleft:
			result = R.drawable.copterleft2;
			break;
		case R.drawable.copterleft2:
			result = R.drawable.copterleft;
			break;
		case R.drawable.copterright2:
			result = R.drawable.copterright;
			break;
		case R.drawable.copterright:
			result = R.drawable.copterright2;
			break;
			
		}
		imageId = result;
		return result;
	}
	
	public void updateBlueCounter(Canvas canvas){
		if(blueDir==Direction.RIGHT ){
			if(blueX+35 >= WIDTH || bcollision){
				blueDir = blueDir.getOppositeDirection();
				blueImageId = R.drawable.bluecopterleft;
				blueHelicopter = BitmapFactory.decodeResource(getResources(), R.drawable.bluecopterleft); 
				blueX-=2;
				bcollision = false;
			}
			else{
				blueHelicopter= BitmapFactory.decodeResource(getResources(), getNextBlueImage());
			blueX+=2;
			}
		}
		if(blueDir==Direction.LEFT){
			if(blueX-5 < 0 || bcollision){
				blueDir=blueDir.getOppositeDirection();
				blueImageId = R.drawable.bluecopterright;
				blueHelicopter = BitmapFactory.decodeResource(getResources(), R.drawable.bluecopterright); 
				blueX+=2;
				bcollision = false;
			}
			else{
				blueHelicopter = BitmapFactory.decodeResource(getResources(), getNextBlueImage());
				blueX-=2;
			}
		}
		if(blueDir==Direction.DOWN){
			if(blueY+40 > HEIGHT|| bcollision){
				blueDir=blueDir.getOppositeDirection();
				blueImageId = R.drawable.bluecopterup;
				blueHelicopter = BitmapFactory.decodeResource(getResources(), R.drawable.bluecopterup); 
				blueY-=2;
				bcollision = false;
			}
			else{
				blueHelicopter = BitmapFactory.decodeResource(getResources(), getNextBlueImage());
				blueY+=2;
			}
		}
		if(blueDir==Direction.UP){
			if(blueY-5 < 0 || bcollision){
				blueDir=blueDir.getOppositeDirection();
				blueImageId = R.drawable.bluecopterdown;
				blueHelicopter = BitmapFactory.decodeResource(getResources(), R.drawable.bluecopterdown); 
				blueY+=2;
				bcollision = false;
			}
			else{
				blueHelicopter = BitmapFactory.decodeResource(getResources(), getNextBlueImage());
				blueY-=2;
			}
		}
		
	}
	
	public int getNextBlueImage(){
		int result = 0;
		switch(blueImageId){
		case R.drawable.bluecopterdown:
			result = R.drawable.bluecopterdown2;
			break;
		case R.drawable.bluecopterdown2:
			result = R.drawable.bluecopterdown;
			break;
		case R.drawable.bluecopterup:
			result = R.drawable.bluecopterup2;
			break;
		case R.drawable.bluecopterup2:
			result = R.drawable.bluecopterup;
			break;
		case R.drawable.bluecopterleft:
			result = R.drawable.bluecopterleft2;
			break;
		case R.drawable.bluecopterleft2:
			result = R.drawable.bluecopterleft;
			break;
		case R.drawable.bluecopterright2:
			result = R.drawable.bluecopterright;
			break;
		case R.drawable.bluecopterright:
			result = R.drawable.bluecopterright2;
			break;
			
		}
		blueImageId = result;
		return result;
	}
	
	public void updateGreenCounter(Canvas canvas){
		if(greenDir==Direction.DOWN){
			if(greenY+40 > HEIGHT || greenCollision){
				greenDir=greenDir.getOppositeDirection();
				greenImageId = R.drawable.greencopterup;
				greenHelicopter = BitmapFactory.decodeResource(getResources(), R.drawable.greencopterup); 
				greenY-=2;
				greenCollision = false;
			}
			else{
				greenHelicopter = BitmapFactory.decodeResource(getResources(), getNextGreenImage());
				greenY+=2;
			}
		}
		if(greenDir==Direction.UP){
			if(greenY-5 < 0 || greenCollision){
				greenDir=greenDir.getOppositeDirection();
				greenImageId = R.drawable.greencopterdown;
				greenHelicopter = BitmapFactory.decodeResource(getResources(), R.drawable.greencopterdown); 
				greenY+=2;
				greenCollision= false;
			}
			else{
				greenHelicopter = BitmapFactory.decodeResource(getResources(), getNextGreenImage());
				greenY-=2;
			}
		}
		
	}
	
	public int getNextGreenImage(){
		int result = 0;
		switch(greenImageId){
		case R.drawable.greencopterdown:
			result = R.drawable.greencopterdown2;
			break;
		case R.drawable.greencopterdown2:
			result = R.drawable.greencopterdown;
			break;
		case R.drawable.greencopterup:
			result = R.drawable.greencopterup2;
			break;
		case R.drawable.greencopterup2:
			result = R.drawable.greencopterup;
			break;
		} 
		greenImageId = result;
		return result;
	}

}
